package com.lawyer.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lawyer.entity.Config;
import com.lawyer.model.Message;
import com.lawyer.model.MessageConstant;
import com.lawyer.repository.ConfigRepository;
import com.lawyer.uitls.BeanUtils;

@RestController
@RequestMapping(value = "/system")
@Api(tags = "系统管理-配置")
@SuppressWarnings("all")
public class SystemConfigController {

	protected final Logger logger = LoggerFactory.getLogger(this.getClass());


	@Autowired
	private ConfigRepository configRepository;

	@ApiOperation(httpMethod = "GET", value = "查询所有配置信息", notes = "查询所有配置信息")
	@RequestMapping(value = "/config", method = RequestMethod.GET)
	public HttpEntity<?> config() {
		Message msg = new Message();
		msg.setData(configRepository.findAll());
		return new ResponseEntity<>(msg, HttpStatus.OK);

	}

	@ApiOperation(httpMethod = "GET", value = "配置详情信息", notes = "配置详情信息")
	@RequestMapping(value = "/configInfo", method = RequestMethod.GET)
	public HttpEntity<?> configInfo(Integer id) {
		Message msg = new Message();
		msg.setData(configRepository.findOne(id));
		return new ResponseEntity<>(msg, HttpStatus.OK);

	}
	

	@ApiOperation(httpMethod = "DELETE", value = "删除配置", notes = "删除配置")
	@RequestMapping(value = "/deleteConfig", method = RequestMethod.DELETE)
	public HttpEntity<?> deleteConfig(Integer id) {
		Message msg = new Message();
		configRepository.delete(id);
		return new ResponseEntity<>(msg, HttpStatus.OK);

	}


	@ApiOperation(httpMethod = "POST", value = "修改配置信息", notes = "修改配置信息")
	@RequestMapping(value = "/updateConfig", method = RequestMethod.POST)
	public HttpEntity<?> updateConfig(@RequestBody Config config) {
		Message msg = new Message();
		try {
			config.setName(null);
			Config findConfig = configRepository.findOne(config.getId());
			BeanUtils.copyPropertiesIgnoreNullValue(config, findConfig);
			configRepository.save(findConfig);

		} catch (Exception e) {
			e.printStackTrace();
			msg.setCode(MessageConstant.FAILED_CODE);
			msg.setMessage(MessageConstant.FAILED_MSG);
		}

		return new ResponseEntity<>(msg, HttpStatus.OK);
	}

}
